package com.google.server;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.client.MarketingService;
import com.google.client.Product;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class MarketingServiceImpl extends RemoteServiceServlet implements MarketingService{
	/**
	 * 
	 */
	private static final long serialVersionUID = -1906661621628734436L;
	
	private List<Product> productList = new ArrayList<Product>();

	public boolean insertProduct(String name, String desc,double price,int discount,String img){
    	PersistenceManager pm = PMF.get().getPersistenceManager();
		try {
	    	Product p = new Product(name, price, discount, img, desc);
	    	pm.makePersistent(p);
	    	productList.add(p);
	    } finally {
	      pm.close();
	    }
	return true; 
		
	}
	
	public List<Product> getProductList() {	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			String query = "select from " + Product.class.getName();
			productList = (List<Product>) pm.newQuery(query).execute();
		}finally {
	      pm.close();
	    }		
		return productList;	
	}


	public Product getProduct(long pid) {
		for (Object object : productList) {
			if (((Product) object).getPid() == pid)
				return ((Product) object);
		}
		return null;
	}
}
